package com.mofeng.myshop.domain.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.mofeng.myshop.domain.po.Commodity;
import com.mofeng.myshop.domain.po.CommodityCategory;
import com.mofeng.myshop.domain.po.Project;
import com.mofeng.myshop.domain.vo.base.BaseVo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class CommodityVo extends BaseVo {
    @Schema(description = "商品图片路径")
    private String image;
    @Schema(description = "小图标")
    private String iconImage;
    @Schema(description = "商品代码")
    private String code;
    @Schema(description = "商品基础价格")
    private Double basePrice;
    @Schema(description = "质量倍率,会用(质量数量-1)*此倍率，然后加在原价格上")
    private Double multiplierUp;
    @Schema(description = "等级倍率, 会用等级*此倍率，然后加在原价格上")
    private Double levelUp;
    @Schema(description = "等级上限 没有则空")
    private Integer levelTop;
    @Schema(description = "商品当前价格")
    private Double price;
    @Schema(description = "是否热点商品 0不是 1是")
    private String hot;
    @Schema(description = "折扣，整数 小于等于0则没有")
    private Double discount;
    @Schema(description = "商城ID")
    private String projectId;
    @Schema(description = "商城名称")
    private String projectName;
    @Schema(description = "商品类别名称")
    private String commodityCategoryName;
    @Schema(description = "商品类别ID")
    private String commodityCategoryId;
    @Schema(description = "虚拟删除状态， 0 存在  1删除")
    @TableField("`delete`")
    private String delete;

    private CommodityCategoryVo commodityCategory;

    private ProjectVo project;

    public CommodityVo(Commodity commodity) {
        super(commodity);
        this.image = commodity.getImage();
        this.iconImage = commodity.getIconImage();
        this.code = commodity.getCode();
        this.basePrice = commodity.getBasePrice();
        this.multiplierUp = commodity.getMultiplierUp();
        this.levelUp = commodity.getLevelUp();
        this.levelTop = commodity.getLevelTop();
        this.price = commodity.getPrice();
        this.hot = commodity.getHot();
        this.discount = commodity.getDiscount();
        this.delete = commodity.getDelete();
        this.commodityCategoryId = commodity.getCommodityCategoryId() == null ? "" : commodity.getCommodityCategoryId().toString();
        CommodityCategory commodityCategoryEntity = commodity.getCommodityCategory();
        if (commodityCategoryEntity != null) {
            this.commodityCategoryName = commodityCategoryEntity.getName();
            commodityCategory = new CommodityCategoryVo(commodityCategoryEntity);
        }
        this.projectId = commodity.getProjectId() == null ? "" : commodity.getProjectId().toString();
        Project projectEntity = commodity.getProject();
        if (projectEntity != null) {
            this.projectName = projectEntity.getName();
            this.project = new ProjectVo(projectEntity);
        }
    }

    public static List<CommodityVo> change(Collection<Commodity> commodities) {
        return commodities.stream().map(CommodityVo::new).collect(Collectors.toList());
    }
}
